<?php

class SiteController extends Controller
{
    private $_identity;
    public $rememberMe;

    public function actionIndex()
    {
        $this->render('index');
    }

    public function actionHome(){
        $this->render("home");
    }

    public function actionLoginout(){
        Yii::app()->user->logout();
        $this->redirect("/index.html");

//        $this->redirect(Yii::app()->homeUrl);
//        unset(Yii::app()->session['user']);
//        $this->redirect("/index.html");
    }

    public function actionLogin(){
        $username = $_REQUEST['username'];
        $password = $_REQUEST['password'];

        $this->_identity=new UserIdentity($username,$password);
        $this->_identity->authenticate();
        if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
        {
            $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
            Yii::app()->user->login($this->_identity,$duration);
            echo json_encode(Appcode::success);
        }else if($this->_identity->errorCode===UserIdentity::ERROR_USERNAME_INVALID){
            echo json_encode(Appcode::error_username);
        }else{
            echo json_encode(Appcode::error_password);
        }
    }

    /**
     * 用户个人设置界面
     */
    public function actionConfig(){
        if(Yii::app()->user->checkAccess('personalconfig_config'))
        {
            $user_id = Yii::app()->user->user_id;
            $userinfo = $this->connection->createCommand("select * from crm_user where user_id = :user_id")->bindParam(':user_id', $user_id, PDO::PARAM_INT)
                ->queryRow();

            $this->render("config",['userinfo'=>$userinfo]);
        }else{
            echo json_decode(Appcode::error_noauthority);
        }
    }

    public function actionSaveconfig(){
        if(Yii::app()->user->checkAccess('personalconfig_config'))
        {
            //保存
            $user_id = $_REQUEST['user_id'];
            $userimage = $_REQUEST['userimage'];
            $password = $_REQUEST['password'];

            //修改密码，头像
            $this->connection->createCommand("update crm_user set userimage = :userimage where user_id = :user_id")
                ->bindParam(':user_id', $user_id, PDO::PARAM_INT)
                ->bindParam(':userimage', $userimage, PDO::PARAM_STR)
                ->execute();

            if(!is_null($password) && $password!=""){
                $this->connection->createCommand("update crm_user set password = :password where user_id = :user_id")
                    ->bindParam(':user_id', $user_id, PDO::PARAM_INT)
                    ->bindParam(':password', $password, PDO::PARAM_STR)
                    ->execute();
            }

            echo json_encode(Appcode::success);
        }else{
            echo json_decode(Appcode::error_noauthority);
        }
    }

    private function actionRoletest(){
        $auth = Yii::app()->authManager;
        $auth->createOperation('createPost','create a post');
        $auth->createOperation('readPost','read a post');
        $auth->createOperation('updatePost','update a post');
        $auth->createOperation('deletePost','delete a post');
        $bizRule='return Yii::app()->user->id==$params["post"]->authID;';
        $task=$auth->createTask('updateOwnPost','update a post by author himself',$bizRule);
        $task->addChild('updatePost');
        $role=$auth->createRole('reader');
        $role->addChild('readPost');
        $role=$auth->createRole('author');
        $role->addChild('reader');
        $role->addChild('createPost');
        $role->addChild('updateOwnPost');
        $role=$auth->createRole('editor');
        $role->addChild('reader');
        $role->addChild('updatePost');
        $role=$auth->createRole('admin');
        $role->addChild('editor');
        $role->addChild('author');
        $role->addChild('deletePost');
        $auth->assign('reader',1);
        $auth->assign('author',1);
        $auth->assign('editor',1);
        $auth->assign('admin',1);
    }

}